package com.coocaa.coolink_tvos_mobile_demo.ui.recyclelist;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.coocaa.coolink_tvos_mobile_demo.R;

import java.util.List;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

    public interface IItemSelectedListener {
        void onItemRadioButtonSelected(Item item);
        void onItemDeleted(Item item);
    }

    private List<Item> itemList;
    private LifecycleOwner lifecycleOwner;
    private IItemSelectedListener itemListener;

    public ItemAdapter(LiveData<List<Item>> liveItemList,LifecycleOwner lifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner;
        liveItemList.observe(lifecycleOwner, newDataList -> {
            this.itemList = newDataList;
            notifyDataSetChanged();
        });
    }

    public void setItemSelectedListener(IItemSelectedListener listener){
        itemListener = listener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    public int getSelectedPosition() {
        for (int i = 0; i < itemList.size(); i++) {
            if (itemList.get(i).isSelected()) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.textView.setText(item.getName());
        Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
        Typeface normalTypeface = Typeface.defaultFromStyle(Typeface.NORMAL);
        holder.textView.setTypeface(item.isSelected() ? boldTypeface : normalTypeface);
        holder.radioButton.setChecked(item.isSelected());
        holder.radioButton.setOnClickListener(v -> {
            int lastSelectedPosition = getSelectedPosition();
            if (lastSelectedPosition >= 0) {
                Item lastSelectedItem = itemList.get(lastSelectedPosition);
                lastSelectedItem.setSelected(false);
                notifyItemChanged(lastSelectedPosition);
            }
            item.setSelected(true);
            notifyItemChanged(position);
            if(itemListener!=null){
                itemListener.onItemRadioButtonSelected(item);
            }
        });

        holder.deleteButton.setOnClickListener(v -> {
            if(itemListener!=null){
                itemListener.onItemDeleted(item);
            }
        });

        switch (item.getState()) {
            case VISIABLE:
                holder.stateImage.setImageResource(R.drawable.ic_visiable_24dp);
                holder.indicatorText.setText(R.string.indicator_online);
                break;
            case CONNECTED:
                holder.stateImage.setImageResource(R.drawable.ic_connectd_24dp);
                holder.indicatorText.setText(R.string.indicator_connected);

                break;
            case INVISIABLE:
                holder.stateImage.setImageResource(R.drawable.ic_invisiable_24dp);
                holder.indicatorText.setText(R.string.indicator_offline);

                break;
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RadioButton radioButton;
        ImageButton deleteButton;
        ImageView stateImage;
        TextView indicatorText;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view);
            radioButton = itemView.findViewById(R.id.radio_button);
            deleteButton = itemView.findViewById(R.id.delete_button);
            stateImage = itemView.findViewById(R.id.indicator);
            indicatorText = itemView.findViewById(R.id.indicator_text);
        }
    }
}